package com.genexususa.eo.bing.connection;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.*;
import com.genexususa.eo.bing.data.GxException;
import com.genexususa.eo.bing.data.GxItemException;
import com.genexususa.eo.bing.data.Info;
import com.genexususa.eo.bing.data.Util;
import com.genexususa.eo.bing.data.requests.GxTargetCampaignsRequest;
import com.genexususa.eo.bing.data.requests.GxTargetRequest;
import com.genexususa.eo.bing.data.results.*;
import com.genexususa.eo.bing.data.wsSubclasses.*;
import com.genexususa.eo.google.data.GA_Result;
import com.genexususa.eo.google.data.GA_ResultItem;
import org.xml.sax.SAXException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.rpc.ServiceException;
import java.io.IOException;
import java.io.StringWriter;
import java.rmi.RemoteException;
import java.util.HashMap;

public class TargetManager {
    public TargetManager() {
    }

    public String addTargets(long customerAccountId, String targetsRequestsXML) {
        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        AddTargetsToLibraryRequest addTargetsToLibraryRequest = null;
        AddTargetsToLibraryResponse addTargetsToLibraryResponse = null;
        SetTargetToCampaignRequest setTargetToCampaignRequest = null;
        GA_Result result = new GA_Result();
        Util util = new Util();
        GxTargetRequest[] targetRequests = null;
        Target[] targetToSend = null;
        long[] targetIds = null;
        try {
            HashMap<String, String> properties = Info.getBingProperties();
            String namespace = properties.get("campaignNamespace");
            String url = properties.get("campaignURL");
            String userName = properties.get("username");
            String password = properties.get("password");
            String developerToken = properties.get("developerToken");
            String customerId = properties.get("customerId");
            String applicationToken = properties.get("applicationToken");

            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Parse targetRequests from XML
            targetRequests = util.targetsRequestsFromXML(targetsRequestsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            if (targetRequests.length == 0) {
                throw new GxItemException("No TargetRequests were found");
            }

            for (int j=0; j<targetRequests.length; j++) {
                // Set body parameters
                targetToSend = new Target[1];
                targetToSend[0] = targetRequests[j].getTarget();
                addTargetsToLibraryRequest = new AddTargetsToLibraryRequest();
                addTargetsToLibraryRequest.setTargets(targetToSend);

                try {
                    // Call WS
                    addTargetsToLibraryResponse = campaignManagement.addTargetsToLibrary(addTargetsToLibraryRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

                    // Call succeeded with no errors
                    targetIds = addTargetsToLibraryResponse.getTargetIds();

                    // SetTargetToCampaign
                    if (targetIds[j] > 0) {
                        // Set body parameters
                        setTargetToCampaignRequest = new SetTargetToCampaignRequest();
                        setTargetToCampaignRequest.setCampaignId(targetRequests[j].getBingCampaignId());
                        setTargetToCampaignRequest.setTargetId(targetIds[j]);

                        try {
                            // Call WS
                            campaignManagement.setTargetToCampaign(setTargetToCampaignRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

                            // Call succeeded with no errors

                            // Create the return object
                            for (int i=0; i<targetIds.length; i++) {
                                GA_ResultItem resultItem = new GA_ResultItem();

                                resultItem.TargetGxId = ((GxTarget)targetRequests[j].getTarget()).getGxId();
                                resultItem.TargetId = targetIds[i];
                                resultItem.SubCampaignGxId = targetRequests[j].getGxCampaignId();
                                resultItem.CampaignId = targetRequests[j].getBingCampaignId();

                                result.Items.add(resultItem);
                            }
                        } catch (ApiFaultDetail fault) { // IN CALL
                            for (int i=0; i<targetIds.length; i++) {
                                GA_ResultItem resultItem = new GA_ResultItem(fault, i);

                                resultItem.TargetGxId = ((GxTarget)targetRequests[j].getTarget()).getGxId();
                                resultItem.SubCampaignGxId = targetRequests[j].getGxCampaignId();

                                result.Items.add(resultItem);
                            }
                        } catch (AdApiFaultDetail fault) { // IN CALL
                            for (int i=0; i<targetIds.length; i++) {
                                GA_ResultItem resultItem = new GA_ResultItem(fault);

                                resultItem.TargetGxId = ((GxTarget)targetRequests[j].getTarget()).getGxId();
                                resultItem.SubCampaignGxId = targetRequests[j].getGxCampaignId();

                                result.Items.add(resultItem);
                            }
                        } catch (RemoteException e) { // IN CALL
                            for (int i=0; i<targetIds.length; i++) {
                                GA_ResultItem resultItem = new GA_ResultItem(e);

                                resultItem.TargetGxId = ((GxTarget)targetRequests[j].getTarget()).getGxId();
                                resultItem.SubCampaignGxId = targetRequests[j].getGxCampaignId();

                                result.Items.add(resultItem);
                            }
                        }
                    } else {
                        // Ocurrio un error al agregar el target (AddTargetsToLibrary)
                        for (int i=0; i<targetIds.length; i++) {
                            GA_ResultItem resultItem = new GA_ResultItem();

                            resultItem.TargetGxId = ((GxTarget)targetRequests[j].getTarget()).getGxId();
                            resultItem.SubCampaignGxId = targetRequests[j].getGxCampaignId();
                            resultItem.HasError = true;

                            result.Items.add(resultItem);
                        }
                    }
                } catch (ApiFaultDetail fault) { // IN CALL
                    for (int i=0; i<addTargetsToLibraryRequest.getTargets().length; i++) {
                        GA_ResultItem resultItem = new GA_ResultItem(fault, i);

                        resultItem.TargetGxId = ((GxTarget)targetRequests[j].getTarget()).getGxId();
                        resultItem.SubCampaignGxId = targetRequests[j].getGxCampaignId();

                        result.Items.add(resultItem);
                    }
                } catch (AdApiFaultDetail fault) { // IN CALL
                    for (int i=0; i<addTargetsToLibraryRequest.getTargets().length; i++) {
                        GA_ResultItem resultItem = new GA_ResultItem(fault);

                        resultItem.TargetGxId = ((GxTarget)targetRequests[j].getTarget()).getGxId();
                        resultItem.SubCampaignGxId = targetRequests[j].getGxCampaignId();

                        result.Items.add(resultItem);
                    }
                } catch (RemoteException e) { // IN CALL
                    for (int i=0; i<addTargetsToLibraryRequest.getTargets().length; i++) {
                        GA_ResultItem resultItem = new GA_ResultItem(e);
                        resultItem.TargetGxId = ((GxTarget)targetRequests[j].getTarget()).getGxId();
                        resultItem.SubCampaignGxId = targetRequests[j].getGxCampaignId();
                        result.Items.add(resultItem);
                    }
                }
            }
        } catch (GxException e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        } catch (Exception e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        }

        String returnMessage = null;
        // GA_Result -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GA_Result.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }

    public String updateTargetsInLibrary(long customerAccountId, String targetRequestsXML) {
        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        UpdateTargetsInLibraryRequest updateTargetsInLibraryRequest = null;
        GA_Result result = new GA_Result();
        Util util = new Util();
        GxTargetRequest[] targetRequests = null;
        Target[] targetToSend = null;
        try {

            HashMap<String, String> properties = Info.getBingProperties();
            String namespace = properties.get("campaignNamespace");
            String url = properties.get("campaignURL");
            String userName = properties.get("username");
            String password = properties.get("password");
            String developerToken = properties.get("developerToken");
            String customerId = properties.get("customerId");
            String applicationToken = properties.get("applicationToken");

            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Set body parameters
            updateTargetsInLibraryRequest = new UpdateTargetsInLibraryRequest();
            // Parse targets from XML
            targetRequests = util.targetsRequestsFromXML(targetRequestsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            if (targetRequests.length == 0) {
                throw new GxItemException("No Targets were found");
            }
            for (int j=0; j<targetRequests.length; j++) {
                // Set body parameters
                targetToSend = new Target[1];
                targetToSend[0] = targetRequests[j].getTarget();
                updateTargetsInLibraryRequest = new UpdateTargetsInLibraryRequest();
                updateTargetsInLibraryRequest.setTargets(targetToSend);

                try {
                    // Call WS
                    campaignManagement.updateTargetsInLibrary(updateTargetsInLibraryRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

                    // Call succeeded with no errors

                    // Create the return object
                    GA_ResultItem resultItem = new GA_ResultItem();

                    resultItem.TargetGxId = ((GxTarget)targetRequests[j].getTarget()).getGxId();
                    resultItem.TargetId = targetRequests[j].getTarget().getId();
                    resultItem.SubCampaignGxId = targetRequests[j].getGxCampaignId();
                    resultItem.CampaignId = targetRequests[j].getBingCampaignId();

                    result.Items.add(resultItem);
                } catch (ApiFaultDetail fault) { // IN CALL
                    GA_ResultItem resultItem = new GA_ResultItem(fault);

                    resultItem.TargetGxId = ((GxTarget)targetRequests[j].getTarget()).getGxId();
                    resultItem.SubCampaignGxId = targetRequests[j].getGxCampaignId();

                    result.Items.add(resultItem);
                } catch (AdApiFaultDetail fault) { // IN CALL
                    GA_ResultItem resultItem = new GA_ResultItem(fault);

                    resultItem.TargetGxId = ((GxTarget)targetRequests[j].getTarget()).getGxId();
                    resultItem.SubCampaignGxId = targetRequests[j].getGxCampaignId();

                    result.Items.add(resultItem);
                } catch (RemoteException e) { // IN CALL
                    GA_ResultItem resultItem = new GA_ResultItem(e);

                    resultItem.TargetGxId = ((GxTarget)targetRequests[j].getTarget()).getGxId();
                    resultItem.SubCampaignGxId = targetRequests[j].getGxCampaignId();

                    result.Items.add(resultItem);
                }
            }
        } catch (ServiceException e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        } catch (IllegalArgumentException e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        } catch (GxException e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        } catch (ParserConfigurationException e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        } catch (SAXException e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        } catch (GxItemException e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        } catch (IOException e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        }

        String returnMessage = null;
        // GA_Result -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GA_Result.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }

    public String deleteTargetAssociations(long customerAccountId, String targetAssociationsRequestsXML) {/*
        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        DeleteTargetFromCampaignRequest deleteTargetFromCampaignRequest = null;
        GA_Result result = new GA_Result();
        Util util = new Util();
        GxTargetRequest[] targetRequests = null;
        try {

            HashMap<String, String> properties = Info.getBingProperties();
            String namespace = properties.get("campaignNamespace");
            String url = properties.get("campaignURL");
            String userName = properties.get("username");
            String password = properties.get("password");
            String developerToken = properties.get("developerToken");
            String customerId = properties.get("customerId");
            String applicationToken = properties.get("applicationToken");

            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Parse targetCampaignsRequests from XML
            targetRequests = util.targetsRequestsFromXML(targetAssociationsRequestsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            if (targetRequests.length == 0) {
                throw new GxItemException("No TargetCampaignRequests were found");
            }

            // DeleteTargetFromCampaign
            for (int i=0; i<targetRequests.length; i++) {
                // Set body parameters
                deleteTargetFromCampaignRequest = new DeleteTargetFromCampaignRequest();
                deleteTargetFromCampaignRequest.setCampaignId(targetRequests[i].getBingCampaignId());

                try {
                    // Call WS
                    campaignManagement.deleteTargetFromCampaign(deleteTargetFromCampaignRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

                    // Call succeeded with no errors

                    // Create the return object
                    GA_ResultItem resultItem = new GA_ResultItem();

                    resultItem.TargetGxId = ((GxTarget)targetCampaignsRequests[0].getTarget()).getGxId();
                    resultItem.TargetId = targetRequests[j].getTarget().getId();
                    resultItem.SubCampaignGxId = targetRequests[j].getGxCampaignId();
                    resultItem.CampaignId = targetRequests[j].getBingCampaignId();
                } catch (ApiFaultDetail fault) { // IN CALL
                    trackingId = fault.getTrackingId();
                    campaignDeleteResult.getTargetIdCampaignIdResultCollection()[actualDelete].setSpecificTrackingId(trackingId);
                    campaignDeleteResult.getTargetIdCampaignIdResultCollection()[actualDelete].setGxCampaignId(targetCampaignsRequests[i].getGxCampaignId());
                    campaignDeleteResult.getTargetIdCampaignIdResultCollection()[actualDelete].addApiFaultDetailError(fault);
                    campaignDeleteResult.setError();
                } catch (AdApiFaultDetail fault) { // IN CALL
                    trackingId = fault.getTrackingId();
                    campaignDeleteResult.getTargetIdCampaignIdResultCollection()[actualDelete].setSpecificTrackingId(trackingId);
                    campaignDeleteResult.getTargetIdCampaignIdResultCollection()[actualDelete].setGxCampaignId(targetCampaignsRequests[i].getGxCampaignId());
                    campaignDeleteResult.getTargetIdCampaignIdResultCollection()[actualDelete].addAdApiFaultDetailError(fault);
                    campaignDeleteResult.setError();
                } catch (RemoteException e) { // IN CALL
                    campaignDeleteResult.getTargetIdCampaignIdResultCollection()[actualDelete].setGxCampaignId(targetCampaignsRequests[i].getGxCampaignId());
                    campaignDeleteResult.getTargetIdCampaignIdResultCollection()[actualDelete].addRemoteExceptionError(e);
                    campaignDeleteResult.setError();
                }
            }

            // SetTargetToAdGroup

            adGroupDeleteResult = new GxTargetIdAdGroupIdResults(cantAdGroupsToAssociate);
            for (int i=0; i<cantAdGroupsToAssociate; i++) {
                adGroupDeleteResult.addResult(i, new GxTargetIdAdGroupIdResult());
            }

            actualDelete = 0;
            for (int i=0; i<targetCampaignsRequests.length; i++) {
                for (int j=0; j<targetCampaignsRequests[i].getAdGroups().length; j++) {
                    // Set body parameters
                    deleteTargetFromAdGroupRequest = new DeleteTargetFromAdGroupRequest();
                    deleteTargetFromAdGroupRequest.setAdGroupId(targetCampaignsRequests[i].getAdGroups()[j].getId());

                    try {
                        // Call WS
                        campaignManagement.deleteTargetFromAdGroup(deleteTargetFromAdGroupRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

                        // Call succeeded with no errors

                        // Create the return object
                        trackingId = stub.getResponseHeader(namespace, "TrackingId").getValue();
                        adGroupDeleteResult.getTargetIdAdGroupIdResultCollection()[actualDelete].setSpecificTrackingId(trackingId);
                        adGroupDeleteResult.getTargetIdAdGroupIdResultCollection()[actualDelete].setGxAdGroupId(((GxAdGroup) targetCampaignsRequests[i].getAdGroups()[j]).getGxId());
                        adGroupDeleteResult.getTargetIdAdGroupIdResultCollection()[actualDelete].setBingAdGroupId(targetCampaignsRequests[i].getAdGroups()[j].getId());
                        for (int j=0; j<targets.length; j++) {
                            // Set body parameters
                            targetIdToSend = new long[1];
                            targetIdToSend[0] = targets[j].getId();
                            deleteTargetsFromLibraryRequest = new DeleteTargetsFromLibraryRequest();
                            deleteTargetsFromLibraryRequest.setTargetIds(targetIdToSend);

                            try {
                                // Call WS
                                campaignManagement.deleteTargetsFromLibrary(deleteTargetsFromLibraryRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

                                // Call succeeded with no errors

                                // Create the return object
                                trackingId = stub.getResponseHeader(namespace, "TrackingId").getValue();
                                result.getTrackingIdResultCollection()[j].setSpecificTrackingId(trackingId);
                                result.getTrackingIdResultCollection()[j].setGxId(((GxTarget)targets[j]).getGxId());
                                result.getTrackingIdResultCollection()[j].setBingId(targets[j].getId());
                            } catch (ApiFaultDetail fault) { // IN CALL
                                trackingId = fault.getTrackingId();
                                result.getTrackingIdResultCollection()[j].setSpecificTrackingId(trackingId);
                                result.getTrackingIdResultCollection()[j].setGxId(((GxTarget)targets[j]).getGxId());
                                result.getTrackingIdResultCollection()[j].addApiFaultDetailError(fault);
                                result.setError();
                            } catch (AdApiFaultDetail fault) { // IN CALL
                                trackingId = fault.getTrackingId();
                                result.getTrackingIdResultCollection()[j].setSpecificTrackingId(trackingId);
                                result.getTrackingIdResultCollection()[j].setGxId(((GxTarget)targets[j]).getGxId());
                                result.getTrackingIdResultCollection()[j].addAdApiFaultDetailError(fault);
                                result.setError();
                            } catch (RemoteException e) { // IN CALL
                                result.getTrackingIdResultCollection()[j].setGxId(((GxTarget)targets[j]).getGxId());
                                result.getTrackingIdResultCollection()[j].addRemoteExceptionError(e);
                                result.setError();
                            }
                        }
                    } catch (ApiFaultDetail fault) { // IN CALL
                        trackingId = fault.getTrackingId();
                        adGroupDeleteResult.getTargetIdAdGroupIdResultCollection()[actualDelete].setSpecificTrackingId(trackingId);
                        adGroupDeleteResult.getTargetIdAdGroupIdResultCollection()[actualDelete].setGxAdGroupId(((GxAdGroup)targetCampaignsRequests[i].getAdGroups()[j]).getGxId());
                        adGroupDeleteResult.getTargetIdAdGroupIdResultCollection()[actualDelete].addApiFaultDetailError(fault);
                        adGroupDeleteResult.setError();
                    } catch (AdApiFaultDetail fault) { // IN CALL
                        trackingId = fault.getTrackingId();
                        adGroupDeleteResult.getTargetIdAdGroupIdResultCollection()[actualDelete].setSpecificTrackingId(trackingId);
                        adGroupDeleteResult.getTargetIdAdGroupIdResultCollection()[actualDelete].setGxAdGroupId(((GxAdGroup)targetCampaignsRequests[i].getAdGroups()[j]).getGxId());
                        adGroupDeleteResult.getTargetIdAdGroupIdResultCollection()[actualDelete].addAdApiFaultDetailError(fault);
                        adGroupDeleteResult.setError();
                    } catch (RemoteException e) { // IN CALL
                        adGroupDeleteResult.getTargetIdAdGroupIdResultCollection()[actualDelete].setGxAdGroupId(((GxAdGroup)targetCampaignsRequests[i].getAdGroups()[j]).getGxId());
                        adGroupDeleteResult.getTargetIdAdGroupIdResultCollection()[actualDelete].addRemoteExceptionError(e);
                        adGroupDeleteResult.setError();
                    }
                    actualDelete++;
                }
            }
        } catch (ServiceException e) { // BEFORE CALL
            result = new GxTargetResults();
            result.addServiceExceptionError(e);
        } catch (IllegalArgumentException e) { // BEFORE CALL
            result = new GxTargetResults();
            result.addIllegalArgumentExceptionError(e);
        } catch (GxException e) { // BEFORE CALL
            campaignDeleteResult = new GxTargetIdCampaignIdResults(e.getExceptions().length);
            for (int i=0; i<e.getExceptions().length; i++) {
                campaignDeleteResult.addResult(i, new GxTargetIdCampaignIdResult());
            }
            campaignDeleteResult.addGxExceptionError(e);
        } catch (ParserConfigurationException e) { // BEFORE CALL
            result = new GxTargetResults();
            result.addParserConfigurationExceptionError(e);
        } catch (SAXException e) { // BEFORE CALL
            result = new GxTargetResults();
            result.addSAXExceptionError(e);
        } catch (GxItemException e) { // BEFORE CALL
            result = new GxTargetResults();
            result.addGxItemExceptionError(e);
        } catch (IOException e) { // BEFORE CALL
            result = new GxTargetResults();
            result.addIOExceptionError(e);
        }

        result.setCampaignAssociationResultCollection(campaignDeleteResult);
        result.setAdGroupAssociationResultCollection(adGroupDeleteResult);
        result.updateError(false);

        String returnMessage = null;
        // GxTargetResults -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GxTargetResults.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;*/
        return null;
    }

    public String deleteTargetsFromLibrary(long customerAccountId, String targetsXML) {
        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        DeleteTargetsFromLibraryRequest deleteTargetsFromLibraryRequest = null;
        GxTrackingIdResults result = null;
        String trackingId = "";
        Util util = new Util();
        Target[] targets = null;
        long[] targetIdToSend = null;
        try {

            HashMap<String, String> properties = Info.getBingProperties();
            String namespace = properties.get("campaignNamespace");
            String url = properties.get("campaignURL");
            String userName = properties.get("username");
            String password = properties.get("password");
            String developerToken = properties.get("developerToken");
            String customerId = properties.get("customerId");
            String applicationToken = properties.get("applicationToken");

            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Set body parameters
            deleteTargetsFromLibraryRequest = new DeleteTargetsFromLibraryRequest();
            // Parse targets from XML
            targets = util.targetsFromXML(targetsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            if (targets.length == 0) {
                throw new GxItemException("No Targets were found");
            }
            result = new GxTrackingIdResults(targets.length);
            for (int i=0; i<targets.length; i++) {
                result.addResult(i, new GxTrackingIdResult());
            }
            for (int j=0; j<targets.length; j++) {
                // Set body parameters
                targetIdToSend = new long[1];
                targetIdToSend[0] = targets[j].getId();
                deleteTargetsFromLibraryRequest = new DeleteTargetsFromLibraryRequest();
                deleteTargetsFromLibraryRequest.setTargetIds(targetIdToSend);

                try {
                    // Call WS
                    campaignManagement.deleteTargetsFromLibrary(deleteTargetsFromLibraryRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

                    // Call succeeded with no errors

                    // Create the return object
                    trackingId = stub.getResponseHeader(namespace, "TrackingId").getValue();
                    result.getTrackingIdResultCollection()[j].setSpecificTrackingId(trackingId);
                    result.getTrackingIdResultCollection()[j].setGxId(((GxTarget)targets[j]).getGxId());
                    result.getTrackingIdResultCollection()[j].setBingId(targets[j].getId());
                } catch (ApiFaultDetail fault) { // IN CALL
                    trackingId = fault.getTrackingId();
                    result.getTrackingIdResultCollection()[j].setSpecificTrackingId(trackingId);
                    result.getTrackingIdResultCollection()[j].setGxId(((GxTarget)targets[j]).getGxId());
                    result.getTrackingIdResultCollection()[j].addApiFaultDetailError(fault);
                    result.setError();
                } catch (AdApiFaultDetail fault) { // IN CALL
                    trackingId = fault.getTrackingId();
                    result.getTrackingIdResultCollection()[j].setSpecificTrackingId(trackingId);
                    result.getTrackingIdResultCollection()[j].setGxId(((GxTarget)targets[j]).getGxId());
                    result.getTrackingIdResultCollection()[j].addAdApiFaultDetailError(fault);
                    result.setError();
                } catch (RemoteException e) { // IN CALL
                    result.getTrackingIdResultCollection()[j].setGxId(((GxTarget)targets[j]).getGxId());
                    result.getTrackingIdResultCollection()[j].addRemoteExceptionError(e);
                    result.setError();
                }
            }
        } catch (ServiceException e) { // BEFORE CALL
            result = new GxTrackingIdResults();
            result.addServiceExceptionError(e);
        } catch (IllegalArgumentException e) { // BEFORE CALL
            result = new GxTrackingIdResults();
            result.addIllegalArgumentExceptionError(e);
        } catch (GxException e) { // BEFORE CALL
            result = new GxTrackingIdResults(e.getExceptions().length);
            for (int i=0; i<e.getExceptions().length; i++) {
                result.addResult(i, new GxTrackingIdResult());
            }
            result.addGxExceptionError(e);
        } catch (ParserConfigurationException e) { // BEFORE CALL
            result = new GxTrackingIdResults();
            result.addParserConfigurationExceptionError(e);
        } catch (SAXException e) { // BEFORE CALL
            result = new GxTrackingIdResults();
            result.addSAXExceptionError(e);
        } catch (GxItemException e) { // BEFORE CALL
            result = new GxTrackingIdResults();
            result.addGxItemExceptionError(e);
        } catch (IOException e) { // BEFORE CALL
            result = new GxTrackingIdResults();
            result.addIOExceptionError(e);
        }

        String returnMessage = null;
        // GxTrackingIdResults -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GxTrackingIdResults.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }
}
